package com.blueheart.Test.list;

public class LinkedList2<E> extends AbstractList<E> {

    private Node first;
    private Node last;

    private static class Node<E> {
        Node<E> pre;
        Node<E> next;
        E element;
        public Node(Node pre,Node next,E element){
            this.pre = pre;
            this.next = next;
            this.element = element;
        }
    }



    @Override
    public E get(int index) {
        checkElementIndex(index);
        return node(index).element;
    }

    private void checkElementIndex(int index) {
        if(!isElementIndex(index)){
            throw new IndexOutOfBoundsException(": Index:"+index+" ，Size:"+size);
        }
    }

    private boolean isElementIndex(int index) {
        return index >=0 && index < size;
    }

    private Node<E> node(int index) {
        Node x = first;
        if(index > size >> 1){
            //按一半进行区分，
            x = last;
            for(int i = size - 1;i > index;i--){
                x = x.pre;
            }
        }else {
            for (int i = 0; i < index; i++) {
                x = x.next;
            }
        }
        return x;
    }

    @Override
    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> node = node(index);
        E element1 = node.element;
        node.element = element;
        return element1;

    }

    @Override
    public void add(int index, E element) {
        checkPositionIndex(index);
//        if(index == 0){
//            first = new Node<>(first, element);
//        }else{
//            Node<E> pre = node(index - 1);
//            Node<E> next = pre.next;
//            pre.next = new Node<E>(next,element);
//        }
        size++;
    }

    private void checkPositionIndex(int index) {
        if(!isPositionIndex(index)){
            throw new IndexOutOfBoundsException(": Index:"+index+" ，Size:"+size);
        }
    }

    private boolean isPositionIndex(int index) {
        return index >=0 && index <= size;
    }

    @Override
    public E remove(int index) {
        checkElementIndex(index);
        Node<E> oldNode = first;
        if(index==0){
            first  = first.next;
        }else{
            Node<E> pre = node(index-1);
            oldNode = pre.next;
            pre.next = oldNode.next;
        }
        size--;
        return oldNode.element;
    }

    @Override
    public int indexOf(E element) {
        //要考虑List可以以为null的情况所以比较要分成两种情况1.element是null
        Node x = first;
        int index = 0;
        if(element == null){
            //使用==比较
            for(Node i=x;i!=null;i=i.next){
                if(i.element == element){
                    return index;
                }
                index++;
            }
        }else{
            //使用equals比较
            for(Node i=x;i!=null;i=i.next){
                //由于List元素可以为null,所以不能反过来写，有空指针的风险
                if(element.equals(i.element)){
                    return index;
                }
                index++;
            }
        }
        return index;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public String toString() {
      if(size == 0){
          return "[]";
      }
      StringBuilder sb = new StringBuilder().append("[");
      Node x = first;
        for(Node i=x;i!=null;i=i.next){
           sb.append(i.element);
           if(i.next==null){
               return sb.append("]").toString();
           }
           sb.append(",");
        }
      return "";


    }
}
